package com.lagace.spark.twitter;

import java.util.Calendar;

import junit.framework.TestCase;

public class TwitterUtilTest extends TestCase {
	
	long currentTimeMillis;
	long lessThanOneMinuteAgo;
	long oneMinuteAgo;
	long minutesAgo;
	long oneHourAgo;
	long hoursAgo;
	long oneDayAgo;
	long daysAgo;
	long oneWeekAgo;
	long weeksAgo;
	
	protected void setUp() throws Exception {
		setupTimeData();
	}

	public void testDoesNotInsertAnchorsWhenHttpNotPresent(){
		String textWithHttp = "Not to be linked www.foo.com";
		assertEquals(textWithHttp, TwitterUtil.insertAnchors(textWithHttp));
	}
	
	public void testDoesNotInsertAnchorsWhenNullMessagePassed(){
		assertEquals("", TwitterUtil.insertAnchors(null));
	}

	public void testInsertsAnchorsWhenHttpPresent(){
		String textWithHttp = "This is my link: http://www.foo.com";
		String expectedResult = "This is my link: " +
								TwitterUtil.ANCHOR_BEGIN_OPEN_BRACKET +
								"http://www.foo.com" +
								TwitterUtil.ANCHOR_BEGIN_CLOSE_BRACKET +
								"http://www.foo.com" +
								TwitterUtil.ANCHOR_END + TwitterUtil.SPACE;
		assertEquals(expectedResult, TwitterUtil.insertAnchors(textWithHttp));
	}
	
	public void testInsertsAnchorsWhenOnlyHttpInMessage(){
		String textWithHttp = "http://www.foo.com";
		String expectedResult = TwitterUtil.ANCHOR_BEGIN_OPEN_BRACKET +
								"http://www.foo.com" +
								TwitterUtil.ANCHOR_BEGIN_CLOSE_BRACKET +
								"http://www.foo.com" +
								TwitterUtil.ANCHOR_END + TwitterUtil.SPACE;
		assertEquals(expectedResult, TwitterUtil.insertAnchors(textWithHttp));
	}
	
	public void testIsNotBlank(){
		assertTrue(TwitterUtil.isNotBlank("foo"));
		assertFalse(TwitterUtil.isNotBlank(null));
		assertFalse(TwitterUtil.isNotBlank(""));
		assertFalse(TwitterUtil.isNotBlank(" "));
		assertFalse(TwitterUtil.isNotBlank("    "));
	}

	public void testGeneratesReadableStatusTimeFormat(){
		assertEquals("Less than 1 minute ago", TwitterUtil.toTimelineFormat(lessThanOneMinuteAgo, currentTimeMillis));
		assertEquals("1 minute ago", TwitterUtil.toTimelineFormat(oneMinuteAgo, currentTimeMillis));
		assertEquals("59 minutes ago", TwitterUtil.toTimelineFormat(minutesAgo, currentTimeMillis));
		assertEquals("1 hour ago", TwitterUtil.toTimelineFormat(oneHourAgo, currentTimeMillis));
		assertEquals("23 hours ago", TwitterUtil.toTimelineFormat(hoursAgo, currentTimeMillis));
		assertEquals("1 day ago", TwitterUtil.toTimelineFormat(oneDayAgo, currentTimeMillis));
		assertEquals("6 days ago", TwitterUtil.toTimelineFormat(daysAgo, currentTimeMillis));
		assertEquals("1 week ago", TwitterUtil.toTimelineFormat(oneWeekAgo, currentTimeMillis));
		assertEquals("4 weeks ago", TwitterUtil.toTimelineFormat(weeksAgo, currentTimeMillis));
	}

	private void setupTimeData() {
		Calendar c = Calendar.getInstance();
		c.set(2007, Calendar.DECEMBER, 2, 8, 45, 0);
		currentTimeMillis = c.getTimeInMillis();
		c.set(2007, Calendar.DECEMBER, 2, 8, 44, 30);
		lessThanOneMinuteAgo = c.getTimeInMillis();
		c.set(2007, Calendar.DECEMBER, 2, 8, 43, 30);
		oneMinuteAgo = c.getTimeInMillis();
		c.set(2007, Calendar.DECEMBER, 2, 7, 46, 00);
		minutesAgo = c.getTimeInMillis();
		c.set(2007, Calendar.DECEMBER, 2, 7, 45, 0);
		oneHourAgo = c.getTimeInMillis();
		c.set(2007, Calendar.DECEMBER, 1, 9, 45, 0);
		hoursAgo = c.getTimeInMillis();
		c.set(2007, Calendar.DECEMBER, 1, 8, 45, 0);
		oneDayAgo = c.getTimeInMillis();
		c.set(2007, Calendar.NOVEMBER, 26, 8, 45, 0);
		daysAgo = c.getTimeInMillis();
		c.set(2007, Calendar.NOVEMBER, 25, 8, 45, 0);
		oneWeekAgo = c.getTimeInMillis();
		c.set(2007, Calendar.NOVEMBER, 4, 8, 45, 0);
		weeksAgo = c.getTimeInMillis();
	}
}
